package com.markus.code.图.图1;

/**
 * Author:markusZhang
 * Date:Create in 2020/8/18 21:21
 * todo: 图的生成
 */
public class GraphGenerator {
    public static Graph createGraph(int [][]matrix){
        Graph graph = new Graph();
        for (int i=0;i<matrix.length;i++){
            Integer weight = matrix[i][0];
            Integer from = matrix[i][1];
            Integer to = matrix[i][2];
            //如果不存在就创建一下
            if (!graph.nodes.containsKey(from)){
                graph.nodes.put(from,new Node(from));
            }
            if (!graph.nodes.containsKey(to)){
                graph.nodes.put(to,new Node(to));
            }
            Node fromNode = graph.nodes.get(from);
            Node toNode = graph.nodes.get(to);
            Edge edge = new Edge(weight,fromNode,toNode);
            graph.edges.add(edge);
            fromNode.nexts.add(toNode);
            fromNode.edges.add(edge);
            fromNode.out++;
            toNode.in++;
        }
        return graph;
    }
    public static Graph createNoGraph(int [][]matrix){
        Graph graph = new Graph();
        for (int i=0;i<matrix.length;i++){
            Integer weight = matrix[i][0];
            Integer from = matrix[i][1];
            Integer to = matrix[i][2];
            //如果不存在就创建一下
            if (!graph.nodes.containsKey(from)){
                graph.nodes.put(from,new Node(from));
            }
            if (!graph.nodes.containsKey(to)){
                graph.nodes.put(to,new Node(to));
            }
            Node fromNode = graph.nodes.get(from);
            Node toNode = graph.nodes.get(to);
            Edge edge = new Edge(weight,fromNode,toNode);
            graph.edges.add(edge);
            fromNode.nexts.add(toNode);
            toNode.nexts.add(fromNode);
            fromNode.edges.add(edge);
            toNode.edges.add(edge);
            fromNode.out++;
            toNode.out++;
            fromNode.in++;
            toNode.in++;
        }
        return graph;
    }
}
